<?php

namespace App\Admin\Forms;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;
class CustomHtmlCode extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {


        if(isset($input['html_path']) && $input['html_path']){

            file_put_contents(public_path('uploads/'.$input['html_path']),\Illuminate\Support\Arr::get($input,'html_code_content',''));

        }


        return $this
				->response()
				->success('Processed successfully.')
				->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->textarea('html_code_content','代码')->rows(20)->required();
        $this->hidden('html_path');
        $this->disableResetButton();
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        $html_code_content = '';
        $html_path = '';
        if(isset($this->payload['html_path'])){
            $html_path = str_replace('.zip','/index.html',$this->payload['html_path']);

            $html_code_content = file_get_contents(public_path('uploads/'.$html_path));
        }

        return [
            'html_code_content'  => $html_code_content,
            'html_path'=> $html_path,

        ];
    }


}
